package com.luisliuyi.demo.handler;

public class Looper {
    static ThreadLocal<Looper> sThreadLocal = new ThreadLocal<>();
    MessageQueue messageQueue;
    private Looper() {
        messageQueue = new MessageQueue();
    }
    public static Looper myLooper() {
        return sThreadLocal.get();
    }
    public static void  prepare() {
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        sThreadLocal.set(new Looper());
    }
    public static  void looper() {
        final Looper me = myLooper();
        final MessageQueue queue = me.messageQueue;
        for (; ; ) {
            Message msg=   queue.next();
            msg.target.handleMessage(msg);
        }
    }
}
